﻿@page "/runing/{id}"
@inject IHttpClientFactory httpClientFactory
@inject ISqlSugarClient db

@* @attribute [Authorize] *@
<AuthorizeView>
    <Authorized>
        <div class="dmain-main" style="z-index:1;width:100%">
            @if (FunctionPage != null && FunctionPage.MainPage != null)
            {
                <CascadingValue Value="FunctionPage.MainPage" Name="MainPage" IsFixed="false">
                    <ControlComponentRun Data="FunctionPage.MainPage.Controlmain" ParentData="FunctionPage.MainPage.Controlmain"></ControlComponentRun>
                </CascadingValue>
            }
            else
            {
                <span>未获取到页面,请新建或者选择一个页面</span>
            }
        </div>
    </Authorized>
    <NotAuthorized>
        <Redirect Url="/login"></Redirect>
    </NotAuthorized>
</AuthorizeView>


@code {
    /// <summary>
    ///
    /// </summary>
    [Parameter]
    public string id { get; set; } = "-1";
    public FunctionPage? FunctionPage { get; set; }
    [CascadingParameter(Name = "router")]
    public List<FunctionGroup>? FunctionGroups { get; set; }
    [CascadingParameter]
    private Task<AuthenticationState>? authenticationState { get; set; }
    protected override void OnInitialized()
    {
        System.Console.WriteLine("run-start"+DateTime.Now);
        base.OnInitialized();
    }
    protected override async Task OnInitializedAsync()
    {
        if (authenticationState is not null)
        {
            var authState = await authenticationState;
            var user = authState?.User;
            if (user?.Identity is not null && user.Identity.IsAuthenticated)
            {
                if (FunctionGroups != null)
                {
                    foreach (var item in FunctionGroups)
                    {
                        if (item != null && item.FunctionPages != null)
                        {
                            foreach (var item2 in item.FunctionPages)
                            {
                                if (item2.Id.ToString() == id)
                                {
                                    FunctionPage = item2;
                                    if (FunctionPage != null && FunctionPage.MainPage != null)
                                    {
                                        FunctionPage.MainPage.StateHasChanged = StateHasChanged;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
        await  base.OnInitializedAsync();
    }

    protected override Task OnParametersSetAsync()
    {
        return base.OnParametersSetAsync();
    }
    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {

        }
        base.OnAfterRender(firstRender);
        System.Console.WriteLine("run-end" + DateTime.Now);

    }
    public void onclik()
    {
        using HttpClient http = httpClientFactory.CreateClient("http");
        var ss = http.BaseAddress;
    }
}
